﻿//  Created by Mathew Purchase.
//  Copyright (c) 2014 Mathew Purchase. All rights reserved.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class UI_MarketWindow : MonoBehaviour 
{

	//---------------------------------------------------------------------------------------------
	// Properties
	int activePanels;
	
	
	//---------------------------------------------------------------------------------------------
	// Members

	public GameObject m_scrollPanel;
	public GameObject m_itemPanel;
	public List<UI_Item> m_inventoryPanels;
	public ItemService m_itemMain;
	public Inventory m_settlementInventory;
	public Character m_playerMain;
	public Text m_maxCarry;
	public Text m_carry;
	public Text m_playerGold;
	
	//---------------------------------------------------------------------------------------------
	// Events and Delegates
	
	
	//---------------------------------------------------------------------------------------------
	// Init
	void Start()
	{
		// currently this is broken. Need to get the inventory of the local settlement and display it here (should really be able to use the inventory code.)
		
		// For a temp solution, just show all item types and allow player to buy and sell from that. 
	
		// add UI item panels to list.
		
		
		m_playerMain = GameObject.Find ("PlayerCharacter").GetComponent<Character>();
		
		foreach (Transform child in m_scrollPanel.transform)
		{
			m_inventoryPanels.Add(child.GetComponent<UI_Item>());
			
		}
	}
	
	
	//---------------------------------------------------------------------------------------------
	public void ShowWindow ()
	{
	
		//Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, 
		//with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, 
		//which can prevent common UI scaling issues.
		
		
		m_settlementInventory = m_playerMain.m_currentTerrain.GetComponent<Inventory>();
		
		for (int a = 0; a < m_settlementInventory.m_inventoryItems.Count; a++)
		{
			GameObject Panel = Instantiate(m_itemPanel, Vector3.zero, Quaternion.identity) as GameObject;
			
			Panel.GetComponent<RectTransform>().SetParent(m_scrollPanel.transform, false);
			m_inventoryPanels.Add(Panel.GetComponent<UI_Item>());
			
			
			Panel.GetComponent<UI_Item>().m_item = m_settlementInventory.m_inventoryItems[a];
		}
		
		// check to see if item quantity is higher than 0 and show if relevant.
		if (m_inventoryPanels.Count > 0)
		{
			for (int a = 0; a < (m_settlementInventory.m_inventoryItems.Count); a ++)
			{
				if (m_settlementInventory.m_inventoryItems.Count > 0)
				{
					if (m_settlementInventory.m_inventoryItems[a].Quantity == 0)
					{
						m_inventoryPanels[a].gameObject.SetActive(false);
					}
					if (m_settlementInventory.m_inventoryItems[a].Quantity > 0)
					{
						m_inventoryPanels[a].gameObject.SetActive(true);
						activePanels ++;
					}
				}
			}
		}
		
	}
	
	//---------------------------------------------------------------------------------------------
	// Core Functions
	
	void Update () 
	{
		m_maxCarry.text = m_playerMain.maxCarry.ToString();
		m_carry.text = m_playerMain.carry.ToString();
		m_playerGold.text = m_playerMain.gold.ToString() + " GP";
	}
	
	//---------------------------------------------------------------------------------------------//---------------------------------------------------------------------------------------------
	public void CloseWindow()
	{
		for (int a = 0; a < m_inventoryPanels.Count; a++)
		{
			
			Destroy (m_inventoryPanels[a].gameObject);
		}
		m_inventoryPanels.RemoveRange(0 , m_inventoryPanels.Count);
	}

}
